﻿using AutoMapper;
using Sheep_Breeding.Domain;
using Sheep_Breeding.ErrorCode;
using Sheep_Breeding.Infeartructure;
using Sheep_Breeding.Write.Api.DTOSheep;
using Sheep_Breeding.Write.Api.OperationServices.IServices;

namespace Sheep_Breeding.Write.Api.OperationServices.Services
{
    /// <summary>
    /// 羊场管理实现类
    /// </summary>
    public class SheepfarmServices: ISheepfarmServices
    {
        private readonly ILogger<SheepfarmServices> logger;
        private readonly IBaseRepository<Sheepfarm_management> sheepfarm_management;
        private readonly IMapper mapper;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        public SheepfarmServices(ILogger<SheepfarmServices> logger,IBaseRepository<Sheepfarm_management> sheepfarm_management,IMapper mapper)
        {
            this.logger = logger;
            this.sheepfarm_management = sheepfarm_management;
            this.mapper = mapper;
        }

        /// <summary>
        /// 新增羊场信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult> CreateSheepfarm(CreateSheepfarmDto dto)
        {
            try
            {
                logger.LogInformation("新增羊场信息操作");

                var res=new ApiResult();

                var info = mapper.Map<Sheepfarm_management>(dto);

                var model = await sheepfarm_management.AddAsyn(info);
                if (model > 0)
                {
                    res.success("羊场信息新增成功");
                    return res;
                }


                return res;

            }
            catch (Exception ex)
            {

                logger.LogError("羊场信息报错" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 修改羊场信息
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult> UpdateSheepfarm(UpdateSheepfarm dto)
        {
            try
            {
                logger.LogInformation("修改羊场信息操作");

                var res=new ApiResult();


                var info = mapper.Map<Sheepfarm_management>(dto);

                var model = await sheepfarm_management.Update(info);

                if (model > 0)
                {
                    res.success("修改羊场信息成功");
                    return res;
                }

                return res;
            }
            catch (Exception ex)
            {

                logger.LogError("修改羊场信息报错" + ex.Message);
                throw ex;
            }
        }
    }
}
